<?php
namespace app\index\controller;

use app\index\model\Category;
use think\Controller;

class CommonController extends Controller
{
    protected $category = null;
    public function _initialize()
    {
        parent::_initialize();
        if ($this->category == null) $this->category = new Category();
        //1.头部 导航栏目
        $navs = $this->category->index_list();
        $this->assign('navs', $navs);

        //2.取出配置项内容  -- 在extra/web.php中
        $config = \config('web');
        $this->assign('config', $config);

        //3.匹配url，展示面包屑导航
        $url = request()->url();//查询子栏目的id号
        $map = $this->category->url_map($url);
        //修改为空的情况
        $str = $map_name = '';
        if (!empty($map)) {
            if (!is_array($map)) $map = [$map];
            $map_name = end($map)->name;
            $str = '';
            foreach ($map as $v) {
                $str .= '/&nbsp;'.$v->name;
            }
        }
        $this->assign('map_name', $map_name);
        $this->assign('map', $str);
    }

    /**
     *  公共方法 ： 取出block 表的 序列号数据
     * @param string $type
     * @return mixed
     */
    public function index_block($type = '')
    {
        //获取区块内容
        $content = \think\Db::name('block')->field('content')->where('type', $type)->find();
        //反序列化 数据
        return unserialize($content['content']);
    }
}
